package com.jweb.framework.ws.soap.axiom;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.transform.Source;

import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMException;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAPFactory;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.ws.soap.SoapElement;
import org.springframework.xml.transform.StaxSource;
/**
 * 修改人： Thomas
 * 修改内容：新增 
 * 类说明：
 */
public class AxiomSoapElement implements SoapElement {
	
	private final OMElement axiomElement ;
	
	private final SOAPFactory axiomFactory ;
	
	protected AxiomSoapElement(OMElement axiomElement , SOAPFactory axiomFactory){
		Assert.notNull(axiomElement , "'axiomElement' must not be null");
		Assert.notNull(axiomFactory , "axiomFactory must not be null");
		this.axiomElement = axiomElement ;
		this.axiomFactory = axiomFactory ;
	}
	
	public final QName getName(){
		try {
			return axiomElement.getQName();
		} catch (OMException e) {
			throw new AxiomSoapElementException(e);
		}
	}
	
	public final Source getSource(){
		try {
			return new StaxSource(this.axiomElement.getXMLStreamReader());
		} catch (OMException e) {
			throw new AxiomSoapElementException(e);
		}
	}
	
	public final void addAttribute(QName name , String value){
		try {
			OMNamespace namespace = getAxiomFactory().createOMNamespace(name.getNamespaceURI(), name.getPrefix());
			OMAttribute attribute = getAxiomFactory().createOMAttribute(name.getLocalPart(), namespace, value);
			getAxiomElement().addAttribute(attribute);
		} catch (OMException e) {
			throw new AxiomSoapElementException(e);
		}
	}
	
	public void removeAttribute(QName name){
		try {
			OMAttribute attribute = getAxiomElement().getAttribute(name);
			if(attribute != null){
				getAxiomElement().removeAttribute(attribute);
			}
		} catch (OMException e) {
			throw new AxiomSoapElementException(e);
		}
	}
	
	public final String getAttributeValue(QName name){
		try {
			return getAxiomElement().getAttributeValue(name);
		} catch (OMException e) {
			throw new AxiomSoapElementException(e);
		}
	}
	
	
	public final Iterator<QName> getAllAttributes(){
		try {
			List<QName> results = new ArrayList<QName>();
			for(Iterator<?> iterator = getAxiomElement().getAllAttributes();iterator.hasNext();){
				OMAttribute attribute = (OMAttribute) iterator.next();
				results.add(attribute.getQName());
			}
			return results.iterator();
		} catch (OMException e) {
			throw new AxiomSoapElementException(e);
		}
	}
	
	public void addNamespaceDeclaration(String prefix , String namespaceURI) {
		try {
			if(StringUtils.hasLength(prefix)){
				getAxiomElement().declareNamespace(namespaceURI, prefix);
			}
			else {
				getAxiomElement().declareDefaultNamespace(namespaceURI);
			}
		} catch (OMException e) {
			throw new AxiomSoapElementException(e);
		}
	}
	
	protected final OMElement getAxiomElement(){
		return axiomElement ;
	}
	
	protected final SOAPFactory getAxiomFactory(){
		return axiomFactory ;
	}
	

}
